﻿using System.Collections.Generic;
using System;

namespace HREngine.Bots
{
    public partial class Behavior亡语瞎 : Behavior
    {
        private int bonus_enemy = 4;
        private int bonus_mine = 4;

        public override string BehaviorName() { return "亡语瞎"; }
        PenalityManager penman = PenalityManager.Instance;

        public override int getComboPenality(CardDB.Card card, Minion target, Playfield p)
        {
            // 无法选中
            if (target != null && target.untouchable)
            {
                return 100000;
            }
            // 初始惩罚值
            int pen = 0;
            pen += penman.getSilencePenality(card.name, target, p);
            if (p.owncards.Count > 0)
            {
                if (card.cardIDenum == p.owncards[0].card.cardIDenum || card.cardIDenum == p.owncards[p.owncards.Count - 1].card.cardIDenum)
                {
                    pen -= bonus_mine;
                    switch (card.chnName)
                    {
                        case "古尔丹之颅":
                            pen -= bonus_mine * 27;
                            break;
                    }
                }
            }
            switch (card.chnName)
            {
                case "怒火（等级1）":
                    pen += bonus_enemy * 3;
                    break;
                case "金牌猎手克里":
                    if (p.ownMinions.Count == 0) pen += bonus_mine * 2;
                    break;
                case "剃刀野猪":
                    pen += bonus_mine * 2;
                    break;
                case "剃刀沼泽兽王":
                    pen += bonus_mine * 2;
                    break;
                case "暴怒邪吼者":
                    pen -= bonus_mine * 3;
                    break;
                case "幸运币":
                    pen += bonus_mine * 2;
                    if (p.ownMaxMana == 1)
                    foreach (Handmanager.Handcard hc in p.owncards)
                    {
                        if(hc.card.chnName == "獠牙锥刃")
                        {
                            pen += bonus_mine * 10;
                        }
                    }
                    break;
                case "獠牙锥刃":
                    pen -= bonus_mine * 2;
                    break;
                case "魔钢处决者":
                    if(card.type == CardDB.cardtype.MOB)
                    {
                        pen += bonus_mine * 5;
                    }                    
                    break;
                case "曼科里克":
                    pen -= bonus_mine;
                    break;
                case "新生刺头":
                    pen -= bonus_mine;
                    break;
            }
            return pen;
        }

        // 核心，场面值
        public override float getPlayfieldValue(Playfield p)
        {
            if (p.value > -200000) return p.value;
            float retval = 0;
            retval += getGeneralVal(p);
            retval += p.owncarddraw * 5;
            // 危险血线
            int hpboarder = 10;
            // 不考虑法强了
            if (p.enemyHeroName == HeroEnum.mage) retval += 4 * p.enemyspellpower;
            // 抢脸血线
            int aggroboarder = 15;
            retval += getHpValue(p, hpboarder, aggroboarder);
            // 出牌序列数量
            int count = p.playactions.Count;
            int ownActCount = 0;
            // 排序问题！！！！
            for (int i = 0; i < count; i++)
            {
                Action a = p.playactions[i];
                ownActCount++;
                switch (a.actionType)
                {
                    // 英雄攻击
                    case actionEnum.attackWithHero:
                        continue;
                    case actionEnum.useHeroPower:
                    case actionEnum.playcard:
                        break;
                    default:
                        continue;
                }
                switch (a.card.card.chnName)
                {
                    case "亡语者布莱克松":
                        retval -= i;
                        break;
                    case "伊利达雷研习":
                        retval -= i * 4;
                        break;
                    case "古尔丹之颅":
                        retval -= i * 3;
                        break;
                    case "幽灵视觉":
                        retval -= i;
                        break;
                    case "稳固射击":
                        retval += i ;
                        break;
                    case "恩佐斯，深渊之神":
                        retval += i;
                        break;
                }
            }
            // 对手基本随从交换模拟
            retval += enemyTurnPen(p);
            retval -= p.lostDamage;
            retval += getSecretPenality(p); // 奥秘的影响
            retval -= p.enemyWeapon.Angr * 3 + p.enemyWeapon.Durability * 3;

            //p.value = retval;
            return retval;
        }

        // 发现卡的价值
        public override int getDiscoverVal(CardDB.Card card, Playfield p)
        {
            switch (card.chnName)
            {
                case "恩佐斯，深渊之神":
                    return bonus_mine * 10;
                case "吞噬者穆坦努斯":
                    return bonus_mine * 9;
                case "明星学员斯特里娜":
                case "库尔特鲁斯·陨烬":
                    return bonus_mine * 2;
                case "伊利达雷邪刃武士":
                case "复仇之魂":
                    return bonus_mine * 1;
                case "古尔丹之颅":
                    return bonus_mine * 2;
                case "恐惧魔王之咬":
                    if(p.enemyHero.Hp <= 5)
                    {
                        return bonus_mine * 10;
                    }
                    int cnt = 0;
                    foreach(Minion m in p.enemyMinions)
                    {
                        if (m.Hp == 1 || m.divineshild) cnt++;
                    }
                    return cnt * bonus_mine / 2;
                case "邪犬训练师":
                    return bonus_mine * 0;
                case "邪能学说":
                    foreach(Handmanager.Handcard hc in p.owncards)
                    {
                        if(hc.card.race == 15)
                        {
                            return bonus_mine * 2;
                        }
                    }
                    return -1;
                case "滑翔":
                    if(p.owncards.Count < 4 && p.enemyAnzCards > 4)
                    {
                        return bonus_mine * (4 - p.owncards.Count);
                    }
                    return -1;
            }
            return 0;
        }

        // 敌方随从价值 主要等于 （HP + Angr） * 4  
        public override int getEnemyMinionValue(Minion m, Playfield p)
        {
            bool dieNextTurn = false;
            foreach (Minion mm in p.enemyMinions)
            {
                if (mm.handcard.card.chnName == "末日预言者")
                {
                    dieNextTurn = true;
                    break;
                }
            }
            foreach(CardDB.cardIDEnum s in p.ownSecretsIDList)
            {
                if(s == CardDB.cardIDEnum.EX1_610 || s == CardDB.cardIDEnum.VAN_EX1_610)
                {
                    if (m.Hp <= 2)
                    {
                        dieNextTurn = true;
                        break;
                    }
                }
            }
            if (m.destroyOnEnemyTurnEnd || m.destroyOnEnemyTurnStart || m.destroyOnOwnTurnEnd || m.destroyOnOwnTurnStart) dieNextTurn = true;
            if (dieNextTurn)
            {
                return -1;
            }
            if (m.Hp <= 0) return 0;
            int retval = 10;
            if (p.ownHero.Hp < 10) retval += bonus_mine * 3;
            if (m.Angr > 0 || p.enemyHeroStartClass == TAG_CLASS.PRIEST)
                retval += m.Hp * bonus_enemy;
            retval += m.spellpower * bonus_enemy * 3 / 2;
            if (!m.frozen && !m.cantAttack)
            {
                retval += m.Angr * bonus_enemy;
                if (m.windfury) retval += m.Angr * bonus_enemy / 2;
            }
            if (m.silenced) return retval;

            if (m.taunt) retval += 2;
            if (m.divineshild) retval += m.Angr * 2;
            if (m.divineshild && m.taunt) retval += 5;
            if (m.stealth) retval += 2;

            if (m.lifesteal) retval += m.Angr * bonus_enemy;

            if (m.poisonous)
            {
                retval += 4;
                if (p.ownMinions.Count < p.enemyMinions.Count) retval += 10;
            }
            switch (m.handcard.card.chnName)
            {
                // 异能价值每多 1 属性值加权 4 点
                // 异能价值 4 属性值
                case "原野联络人":
                case "奥":
                case "奥的灰烬":
                case "科卡尔驯犬者":
                case "巡游领队":
                case "团伙核心":
                case "前沿哨所":
                case "莫尔杉哨所":
                case "塔姆辛·罗姆":
                case "导师火心":
                case "伊纳拉·碎雷":
                case "暗影珠宝师汉纳尔":
                case "伦萨克大王":
                case "洛卡拉":
                    retval += bonus_enemy * 4;
                    break;
                // 异能价值 2 属性值
                case "剃刀野猪":
                case "剃刀沼泽兽王":
                case "萨特监工":
                case "食人魔巫术师":
                case "甩笔侏儒":
                case "精英牛头人酋长，金属之神":
                case "克欧雷":
                case "雷欧克":
                case "纳兹曼尼织血者":
                case "塞泰克织巢者":
                    retval += bonus_enemy * 2;
                    break;
                // 异能价值 2 属性值
                case "新生刺头":
                case "聒噪怪":
                    if (m.Spellburst)
                        retval += bonus_enemy * 2;
                    break;
                case "铸甲师":
                    retval += 50;
                    break;
                case "末日预言者":
                    break;
            }
            return retval;
        }
        /// <summary>
        /// 我方随从价值
        /// </summary>
        /// <param name="m"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public override int getMyMinionValue(Minion m, Playfield p)
        {
            bool dieNextTurn = false;
            foreach (Minion mm in p.enemyMinions)
            {
                if (mm.handcard.card.chnName == "末日预言者")
                {
                    dieNextTurn = true;
                    break;
                }
            }
            if (m.destroyOnEnemyTurnEnd || m.destroyOnEnemyTurnStart || m.destroyOnOwnTurnEnd || m.destroyOnOwnTurnStart) dieNextTurn = true;
            if (dieNextTurn)
            {
                return -1;
            }
            int retval = 10;
            if (m.Hp <= 0) return 0;
            retval += m.Hp * bonus_mine;
            retval += m.Angr * bonus_mine;
            if (m.Hp <= 1 && !m.divineshild) retval -= (m.Angr - 1) * (bonus_mine - 1);
            // 高攻低血是垃圾
            if (m.Angr > m.Hp + 4) retval -= (m.Angr - m.Hp) * (bonus_mine - 1);
            // 风怒价值
            if ((!m.playedThisTurn || m.rush == 1 || m.charge == 1) && m.windfury) retval += m.Angr;
            // 圣盾价值
            if (m.divineshild) retval += m.Angr * 3;
            // 潜行价值
            if (m.stealth) retval += m.Angr / 2 + 1;
            // 吸血
            if (m.lifesteal) retval += m.Angr / 2 + 1;
            // 圣盾嘲讽
            if (m.divineshild && m.taunt) retval += 4;
            switch (m.handcard.card.chnName)
            {
                case "吞噬软浆怪":
                    retval += bonus_mine * 3;
                    break;
                case "泰兰·弗丁":
                    retval += bonus_mine * 3;
                    break;
                case "金牌猎手克里":
                    retval += bonus_mine * (p.ownMinions.Count -1);
                    break;
                case "奥":
                    retval += bonus_mine * 3;
                    break;
                case "剃刀野猪":
                    if(p.ownMaxMana < 8)
                    foreach (Handmanager.Handcard hc in p.owncards)
                    {
                        if (hc.manacost <= 3 && hc.card.deathrattle)
                        {
                            retval += bonus_mine * 4;
                            break;
                        }
                    }
                    break;
                case "剃刀沼泽兽王":
                    if (p.ownMaxMana < 8)
                    foreach (Handmanager.Handcard hc in p.owncards)
                    {
                        if (hc.manacost <= 4 && hc.card.deathrattle)
                        {
                            retval += bonus_mine * 4;
                            break;
                        }
                    }
                    break;
                case "知名表演者":
                    retval += bonus_mine * 2;
                    break;
                case "战利品贮藏者":
                case "麦田傀儡":
                    retval += bonus_mine ;
                    break;
                case "麻风侏儒":
                    retval += bonus_mine / 2;
                    break;
                case "年轻的女祭司":
                    if (p.ownMinions.Count > 1)
                        retval += bonus_mine * 2;
                    break;
            }
            if (m.dormant > 0)
            {
                retval -= bonus_mine * m.dormant;
            }
            return retval;
        }

        public override int getSirFinleyPriority(List<Handmanager.Handcard> discoverCards)
        {
            
            return -1; //comment out or remove this to set manual priority
            int sirFinleyChoice = -1;
            int tmp = int.MinValue;
            for (int i = 0; i < discoverCards.Count; i++)
            {
                CardDB.cardName name = discoverCards[i].card.name;
                if (SirFinleyPriorityList.ContainsKey(name) && SirFinleyPriorityList[name] > tmp)
                {
                    tmp = SirFinleyPriorityList[name];
                    sirFinleyChoice = i;
                }
            }
            return sirFinleyChoice;
        }
        public override int getSirFinleyPriority(CardDB.Card card)
        {
            return SirFinleyPriorityList[card.name];
        }

        private Dictionary<CardDB.cardName, int> SirFinleyPriorityList = new Dictionary<CardDB.cardName, int>
        {
            //{HeroPowerName, Priority}, where 0-9 = manual priority
            { CardDB.cardName.lesserheal, 0 }, 
            { CardDB.cardName.shapeshift, 6 },
            { CardDB.cardName.fireblast, 7 },
            { CardDB.cardName.totemiccall, 1 },
            { CardDB.cardName.lifetap, 9 },
            { CardDB.cardName.daggermastery, 5 },
            { CardDB.cardName.reinforce, 4 },
            { CardDB.cardName.armorup, 2 },
            { CardDB.cardName.steadyshot, 8 }
        };

       

        public override int getHpValue(Playfield p, int hpboarder, int aggroboarder)
        {
            int offset = 0;
            int retval = 0;
            // 血线安全
            if (p.ownHero.Hp + p.ownHero.armor + offset > hpboarder)
            {
                retval += bonus_enemy * (5 + p.ownHero.Hp + p.ownHero.armor + offset - hpboarder) / 2;
            }
            // 快死了
            else
            {
                retval -= 2 * bonus_enemy * (hpboarder + 1 - offset - p.ownHero.Hp - p.ownHero.armor) ;
            }
            if (p.ownHero.Hp + p.ownHero.armor + offset < 10 && p.ownHero.Hp + p.ownHero.armor + offset > 0)
            {
                retval -= 200 / (p.ownHero.Hp + p.ownHero.armor + offset);
            }
            if (p.ownHero.Hp + offset <= 7 && (p.enemyHeroStartClass == TAG_CLASS.HUNTER || p.enemyHeroStartClass == TAG_CLASS.MAGE))
            {
                retval -= 200;
            }
            if (p.enemyHero.Hp + p.enemyHero.armor >= aggroboarder)
            {
                retval += bonus_enemy * (aggroboarder - p.enemyHero.Hp - p.enemyHero.armor) / 2;
            }
            else
            {
                retval += bonus_enemy * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor) * 3 / 2;
            }
            //if (p.nextTurnWin())
            //{
            //    retval += 200;
            //}
            return retval;
        }

        /// <summary>
        /// 攻击触发的奥秘惩罚
        /// </summary>
        /// <param name="si"></param>
        /// <param name="attacker"></param>
        /// <param name="defender"></param>
        /// <returns></returns>
        public override int getSecretPen_CharIsAttacked(Playfield p, SecretItem si, Minion attacker, Minion defender)
        {
            if (attacker.isHero) return 0;
            int pen = 0;
            // 攻击的基本惩罚
            if (si.canBe_explosive && !defender.isHero)
            {
                pen -= 20;
                //pen += (attacker.Hp + attacker.Angr);
                foreach (SecretItem sii in p.enemySecretList)
                {
                    sii.canBe_explosive = false;
                }
            }
            return pen;
        }

        // 简易模拟对手回合随从交换，粗糙处理，认为对手从大到小依次攻击我方随从
        public override int enemyTurnPen(Playfield p)
        {
            int pen = 0;
            List<Minion> enemyMinions = new List<Minion>(p.enemyMinions.ToArray());
            List<Minion> ownMinions = new List<Minion>(p.ownMinions.ToArray());
            enemyMinions.Sort((a, b) => -(a.poisonous ? 10000 : a.Angr).CompareTo(b.Angr));
            ownMinions.Sort((a, b) => -(getMyMinionValue(a, p) + (a.Hp > 5 ? -100 : 0)).CompareTo(getMyMinionValue(b, p)) + (b.Hp > 5 ? -100 : 0));
            int minCnt = enemyMinions.Count > ownMinions.Count ? ownMinions.Count : enemyMinions.Count;
            for (int i = 0; i < minCnt; i++)
            {
                // 对手可以进行交换
                if ((enemyMinions[i].Angr >= ownMinions[i].Hp || enemyMinions[i].poisonous) && !ownMinions[i].divineshild && !ownMinions[i].stealth)
                {
                    // 攻击前
                    int enemyVal1 = getEnemyMinionValue(enemyMinions[i], p);
                    Minion afterAtk = new Minion(enemyMinions[i]);
                    if (!enemyMinions[i].divineshild)
                    {
                        afterAtk.Hp -= ownMinions[i].Angr;
                        if (ownMinions[i].poisonous) afterAtk.Hp = 0;
                    }
                    else
                    {
                        afterAtk.divineshild = false;
                    }
                    // 攻击后
                    int enemyVal2 = getEnemyMinionValue(afterAtk, p);
                    int myVal = getMyMinionValue(ownMinions[i], p);
                    if (ownMinions[i].handcard.card.chnName == "金牌猎手克里") pen -= 100;
                    //// 对手愿意交换
                    //if (enemyVal1 - enemyVal2 < myVal)
                    //{
                    //    pen -= myVal;
                    //    pen += enemyVal1 - enemyVal2;
                    //}
                }
            }
            return pen;
        }
    }
}